package fr.emse.tatiana;

import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.internal.ide.model.WorkbenchAdapterBuilder;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

    public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }

    @Override
	public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }
    
    @Override
	public void preWindowOpen() {
    	WorkbenchAdapterBuilder.registerAdapters();
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setInitialSize(new Point(1000, 600));
        configurer.setShowCoolBar(false);
        configurer.setShowStatusLine(false);
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        try {
			root.refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (CoreException e1) {
			e1.printStackTrace();
			// shouldn't happen
		}
        IProject project = root.getProject(TatianaConstants.TATIANA_PROJECT_NAME);
		if (!project.exists()) {
			try {
				project.create(null);
				project.open(null);
			} catch (CoreException e) {
				e.printStackTrace(); // assume never happens
			}
		} else {
			try {
				project.open(null);
			} catch (CoreException e) {
				// assume never happens
				e.printStackTrace();
			}
		}
		IFolder cache = project.getFolder("cache");
		if (!cache.exists()) {
			try {
				System.err.println("create cache folder");
				cache.create(true, true, null);
				cache.touch(null);
			} catch (CoreException e) {
				e.printStackTrace(); // assume never happens
			}
		}
    }
    
}
